// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var leftMenu = require("../leftMenu/leftMenu");
	var topMenu = require("../topMenu/topMenu");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var databind = require("databind");
	var css_financingDetailInfo = require("../financing/financingDetailInfo.css");
	var css_deliveryNotice = require("./deliveryNotice.css");
	var js_platform_main = require("./platform_main.js");
	var tools =require("tools");
	var constant = require("constant");
	var html_inventoryList = require("./html_inventoryList.html");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var imgSrc;
	var left;
	var pickupId;
	var financeId;
	var deliveryNoticeId;
	var pickupChecks = [];
	var init = function(page) {
		pickupId = page.params.pickupId || base.getUrlParam("pickupId");
		deliveryNoticeId = page.params.deliveryNoticeId;
		left = page.params.left || base.getUrlParam("left");
		inquirePickupApplicationDetail();
		if(deliveryNoticeId != null){
			$(".button-submit-deliveryNotice").remove();
		}
//		leftMenu.init(6);
		js_platform_main.init(0);
		initEvent();
		router.init();
		$(".left").removeClass("none");
		$("#leftMenu7").find(".leftMenu-item").removeClass("leftMenu-active");
		$("#leftMenu7 .leftMenu li:first-child").addClass("leftMenu-open");
		$("#leftMenu7").find(".leftMenu-item").eq(left).addClass("leftMenu-active");
		
		if (deliveryNoticeId == null) {
			$("#deliveryNotice .print").remove();
		}
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
			//点击首页
		$(".platform_main").unbind("click").click(function(){
			top.location.href = "../main/platform_main.html";
		});
		
		//点击收款列表
		$(".deliveryNoticeList").unbind("click").click(function(){
			router.route("main/deliveryNotice_list.html?left=8", true);
		});
		
		$("#deliveryNotice .print").unbind("click").click(function(){
			window.open("../financing/deliveryNoticePrint.html?deliveryNoticeId=" + deliveryNoticeId);
		});
		
		//复选框
		$("#deliveryNotice").on("click", ".pickupCheck", function(){
			pickupChecks = [];
			$.each($("#deliveryNotice .pickupCheck:checked"), function() {
				pickupChecks.push($(this).parents("tr").attr("inventoryId"));
			});
			if ($("#deliveryNotice .pickupCheck:checked").length == $("#deliveryNotice .pickupCheck").length) {
				$("#deliveryNotice .pickupCheckAll").prop("checked", true);
			} else {
				$("#deliveryNotice .pickupCheckAll").prop("checked", false);
			}
		});
		
		//全选
		$("#deliveryNotice").on("click", ".pickupCheckAll", function(){
			if ($("#deliveryNotice .pickupCheckAll").prop("checked")) {
				$("#deliveryNotice .pickupCheck").prop("checked", true);
			} else {
				$("#deliveryNotice .pickupCheck").prop("checked", false);
			}
			pickupChecks = [];
			$.each($("#deliveryNotice .pickupCheck:checked"), function() {
				pickupChecks.push($(this).parents("tr").attr("inventoryId"));
			});
		});
		
		//点击通知放货
		$(".button-submit-deliveryNotice").on("click",function(){
			
			if (pickupChecks.length <= 0) {
				base.alert(2, "请选择需要通知发货的货箱！");
				return;
			}
			
			$("#deliveryNotice2_dialog .pickIncentories").html("");
			$.each(pickupChecks, function() {
				var str = '<input type="hidden" name="inventoryId" value="' + this + '" />';
				$("#deliveryNotice2_dialog .pickIncentories").append(str);
			});
			
			dialog.showDialog($("#deliveryNotice2_dialog"));
			databind.fillForm($(".deliveryNotice"),{pickupId:pickupId});
			loadLogisticsCompanyPageList("supervisionCompanyName2", "supervisionTpl2");
			inquireSupervisionContractToSupervisionNotice(financeId);
			$("#sure_deliveryNotice1").unbind("click").click(function(){
				databind.submitDom($(".deliveryNotice"),function(){
					base.alert(1, "通知放货成功");
					dialog.hideDialog($("#deliveryNotice2_dialog"));
					js_platform_main.init(0);
					inquireNormalPickupApplicationDetail1();
				});
			});
		});
	};
	
	//查询融资箱货列表
    var inquireNormalPickupApplicationDetail1 = function(){
        base.fnAjax({
            action: 'inquireNormalPickupApplicationDetail.json',
            async:false,
            oData: {
				pickupId: pickupId
			},
            fnSuccess: function (data) {
                if (data.status == 200) {
                	var d = {};
                	d.datas = data.datas[0].containerInventorys;
                	layer.open({
                	  area: ['620px', '640px'], //宽高
					  title: "解锁状态",
					  type: 1,
					  skin: 'layui-layer-demo', //样式类名
					  closeBtn: 1, //不显示关闭按钮
					  anim: 2,
					  shadeClose: true, //开启遮罩关闭
					  content: html_inventoryList,
					  end: function(){
					  	router.route("main/deliveryNotice_list.html?left=9", true);
					  }
					});
                	var tpl = $("#html_inventoryList script").html();
                	var html = juicer(tpl, d);
                	$("#html_inventoryList table").html(html);
                } else {
                    base.alert(3, data.message);
                }
            }
        });
    };

	//绑定物流公司
	var loadLogisticsCompanyPageList = function(bodyDomId, jsTpl) {
		base.fnAjax({
			action: "inquireLogisticsCompanyListByCompanyName.json",
			version: "v1.0",
			async: false,
			oData: {
				pageIndex: 1,
				pageSize: 50
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					var tpl = $("#" + jsTpl).html();
					var html = juicer(tpl, data);
					$("#" + bodyDomId).html(html);

				} else {
					base.alert(3, data.message);
				}
			}
		});

		//下发运输通知   绑定点击物流方事件
		$("#transportCompanyName").change(function() {
			var companyName = $("#transportCompanyName option:selected").text();
			$(".supervisionCompanyNameInput").val(companyName);
		});

		//绑定点击物流方事件
		$("#supervisionCompanyName").change(function() {
			var companyName = $("#supervisionCompanyName option:selected").text();
			$(".supervisionCompanyNameInput").val(companyName);
		});
	}

	var inquirePickupApplicationDetail = function(){
		base.fnAjax({
			action: "inquireNormalPickupApplicationDetail.json",
			oData: {
				pickupId: pickupId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var html;
					var deliveryNotice_pickupDetailTpl = $("#deliveryNotice_pickupDetailTpl").html();
					if (deliveryNotice_pickupDetailTpl != undefined) {
						html = juicer(deliveryNotice_pickupDetailTpl, data);
					}
					$(".tab1").html(html);
					financeId = data.datas[0].financeId;
					
//					var tpl3 = $(".exchangeBulkstockTpl").html(html);//deliveryNotice_pickupDetailTpl
//					var html3 = juicer(tpl3, data);
//					$(".exchangeBulkstockTable").html(html3);
//					
//					var tpl4 = $(".exchangeInventoryTpl").html(html);
//					var html4 = juicer(tpl4, data);
//					$(".exchangeInventoryTable").html(html4);
					
					$("#deliveryNotice").on("click", ".detail", function(){
						router.route("bulkstockTallyDetail/bulkstockTallyDetail.html?inventoryId=" + $(this).parents("tr").attr("inventoryId"));
					});
					
					var oData = {
						pageIndex: pageIndex,
						pageSize: pageSize,
						totalPageCount: totalPageCount,
						totalRecordCount: totalRecordCount,
						financeId: financeId,
						statementSource: "RECEIVABLES"
					};
					tools.loadList("inquireReceiptMoneyStatementPageList.json", $("#deliveryNotice .receiptManageBody"), $("#deliveryNotice .receiptManageTpl"), oData, function(data) {
						$(".receiptManageBody .receptDetail").unbind("click").click(function() {
							var statementSourceId = $(this).parents("tr").attr("statementSourceId");
							router.route("financing/receivableNoticeDetail.html?receivablesId=" + statementSourceId);
						});
					}, $("#pagenation"));
					
					if ($(".exchangeInventoryTable").find("tr").length == 1) {
						$(".exchangeInventoryTable").parent().addClass("none");
					}
					
					if ($(".exchangeBulkstockTable").find("tr").length == 1) {
						$(".exchangeBulkstockTable").parent().addClass("none");
					}
					
					if ($(".receiptManageBody").find("tr").length == 1) {
						$(".receiptManageBody").parent().addClass("none");
					}
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}
	
	//监管通知获取监管合同和监管物流
	var inquireSupervisionContractToSupervisionNotice = function(financeId) {
		base.fnAjax({
			action: "inquireFinanceSupervisionContract.json",
			oData: {
				financeId: financeId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					databind.fillForm("#deliveryNotice2_dialog", data.datas[0]);
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}
	return {
		init: init
	};
});